package com.demo2do.boyle.client.web.controller.customer;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.boyle.client.service.CustomerProfileService;
import com.demo2do.boyle.client.service.CustomerService;
import com.demo2do.boyle.client.service.GeoService;
import com.demo2do.boyle.common.entity.customer.Customer;
import com.demo2do.boyle.common.entity.customer.Sex;
import com.demo2do.boyle.common.entity.geo.Geo;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.utils.StringUtils;
import com.demo2do.core.web.resolver.Secure;

@Controller
@RequestMapping("/customers")
public class CustomerEditController {
	
	private static final Logger logger = LoggerFactory.getLogger(CustomerController.class);
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private CustomerProfileService customerProfileService;
	
	@Autowired
	private GeoService geoService;
	
	
	/**
	 * 
	 * @param customerId
	 * @param sexName
	 * @return
	 */
	@RequestMapping("/{customerId}/edit-sex")
	@ResponseBody
	public String editSex(@Secure(property="id") Integer customerId, @RequestParam("sex") String sexName) {
		Result result = new Result();
		
		try {
			Customer customer = customerService.load(Customer.class, customerId);
			Sex sex = Sex.aliasOf(sexName);
			customerProfileService.editSex(sex, customer);
			result.success().data("customer", customer).message("修改成功");
		} catch (Exception e) {
			
			logger.info("editSex()" + e);
			result.fail().message("服务器异常");
		}
		
		return JsonUtils.toJsonString(result);
	}
	
	/**
	 * 
	 * @param customerId
	 * @param age
	 * @return
	 */
	@RequestMapping("/{customerId}/edit-age")
	@ResponseBody
	public String editAge(@Secure(property="id") Integer customerId, @RequestParam("age") Integer age) {
		Result result = new Result();
		
		try {
			Customer customer = customerService.load(Customer.class, customerId);
			customerProfileService.editAge(age, customer);
			result.success().data("customer", customer).message("修改成功");
		} catch (Exception e) {
			logger.info("editSex()" + e);
			result.fail().message("服务器异常");
		}
		
		return JsonUtils.toJsonString(result);
	}
	
	/**
	 * 
	 * @param customerId
	 * @param nickname
	 * @return
	 */
	@RequestMapping("/{customerId}/edit-nickname")
	@ResponseBody
	public String editNickname(@Secure(property="id") Integer customerId, @RequestParam("nickname") String nickname) {
		Result result = new Result();
		
		try {
			Customer customer = customerService.load(Customer.class, customerId);
			customerProfileService.editNickName(nickname, customer);
			result.success().data("customer", customer).message("修改成功");
		} catch (Exception e) {
			logger.info("editSex()" + e);
			result.fail().message("服务器异常");
		}
		
		return JsonUtils.toJsonString(result);
	}
	
	/**
	 * 
	 * @param customerId
	 * @param code
	 * @return
	 */
	@RequestMapping("/{customerId}/init-area")
	@ResponseBody
	public String initArea(@PathVariable("customerId") Integer customerId, @RequestParam( value = "code", required =false) String code) {
		Result result = new Result();
		
		try {
			List<Geo> provinces = geoService.getProvince();
			List<Geo> cities = new ArrayList<Geo>();
			if(StringUtils.isBlank(code)) {
				cities = geoService.getCities(provinces.get(0).getCode());
			}else {
				cities = geoService.getCities(code);
			}
			
			result.success().data("provinces", provinces).data("cities", cities).message("获取成功");
		} catch (Exception e) {
			logger.info("initArea" + e);
			result.fail().message("服务器错误");
		}
		
		return JsonUtils.toJsonString(result);
	}
	
	/**
	 * 
	 * @param customerId
	 * @param code
	 * @return
	 */
	@RequestMapping("/{customerId}/get-city")
	@ResponseBody
	public String getCityByCode(@PathVariable("customerId") Integer customerId, @RequestParam("code") String code) {
		Result result = new Result();
		try {
			List<Geo> cities = geoService.getCities(code);
			result.success().data("cities", cities).message("获取成功");
		} catch (Exception e) {
			logger.info("getCity" + e);
			result.fail().message("服务器错误");
		}
		
		return JsonUtils.toJsonString(result);
	}
	
	/**
	 * 
	 * @param customerId
	 * @param code
	 * @return
	 */
	@RequestMapping("/{customerId}/edit-area")
	@ResponseBody
	public String editArea(@Secure(property="id") Integer customerId, @RequestParam("code") String code) {
		Result result = new Result();
		try {

			Customer customer = customerService.load(Customer.class, customerId);
			Geo city = geoService.getGeo(code);
			Geo province = geoService.getGeo(city.getBelongs());
			
			customerProfileService.editArea(city, province, customer);
			
			result.success().data("customer", customer).message("修改成功");
			
		} catch (Exception e) {
			logger.info("editArea()" + e);
			result.fail().message("服务器异常");
		}
		
		return JsonUtils.toJsonString(result);
	}
	
}
